package ui.activity.order.presenter;


import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.CancelOrder;
import Bean.OrderBean;
import Bean.OrderParamBean;
import base.BaseBiz;
import coom.MyType;
import event.CloseActEvent;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.order.biz.OrderBiz;
import ui.activity.order.contract.OrderContract;
import util.ToastUtils;

/**
 * Created by chenjian on 19/2/26
 */

public class OrderPresenter implements OrderContract.Presenter {

    OrderContract.View view;
    OrderBiz orderBiz;
    List<OrderBean.DataBean.ItemsBean> list = new ArrayList<>();

    @Inject
    public OrderPresenter(OrderContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        orderBiz = (OrderBiz) biz;
    }


    @Override
    public void getOrderList(boolean isRefresh, String customId, String flag, String isPay) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        OrderParamBean orderParamBean = new OrderParamBean();
        orderParamBean.setFlag(flag);
        orderParamBean.setCustomId(customId);
        orderParamBean.setIsPay(isPay);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(orderParamBean));
        orderBiz.getOrder(requestBody, new BaseBiz.Callback<OrderBean.DataBean>() {
            @Override
            public void onSuccess(OrderBean.DataBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(OrderBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void toPay(String orderId) {

    }

    @Override
    public void cancleOrder(int orderId, String customId, String flag, String isPay) {
        view.loading(true);
        CancelOrder cancelOrder = new CancelOrder();
        cancelOrder.setOrderId(orderId + "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(cancelOrder));
        orderBiz.cancleOrder(requestBody, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                ToastUtils.shortToast("取消订单成功");
                view.loading(false);
                getOrderList(true, customId, flag, isPay);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void checkLogistics(String orderId) {

    }

    @Override
    public void confirmOrder(String orderId, String customId, String flag, String isPay) {
        view.loading(true);
        CancelOrder cancelOrder = new CancelOrder();
        cancelOrder.setOrderId(orderId + "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(cancelOrder));
        orderBiz.confirmOrder(requestBody, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                ToastUtils.shortToast("确认收货成功");
                view.loading(false);
                getOrderList(true, customId, flag, isPay);
                EventBus.getDefault().post(new CloseActEvent(MyType.CompleteOrder, ""));
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });
    }

    @Override
    public void checkDetails(String orderId) {

    }
}
